package com.fofia.iotweb.web.controllers.admin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fofia.iotweb.cache.UserCache;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.framework.dto.ZtreeDto;
import com.fofia.iotweb.web.dbdomain.models.admin.SysRole;
import com.fofia.iotweb.web.dbdomain.services.admin.SysRoleService;
import com.fofia.iotweb.web.dbdomain.services.admin.SysUserService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import com.fofia.iotweb.web.dto.LoginUserDto;
import com.fofia.iotweb.web.dto.ResultAdaptDto;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Mapping("admin/role")
@Controller
public class RoleController extends BaseController {

    @Inject
    private RequestHelper requestHelper;
    @Inject
    private SysRoleService roleService;
    @Inject
    private UserCache userCache;


    @Get
    @Mapping("index")
    @AuthPermissions("admin.role.view")
    public ModelAndView index(){

        return view();
    }

    @Post
    @Mapping("getPageList")
    @AuthPermissions("admin.role.view")
    public DataTableDto getPageList(DataTableDto dto){

        String roleName=requestHelper.getString("roleName","");

        var data=roleService.getPageList(dto.getPageNumber(),dto.getPageSize(),roleName);
        return data;
    }

    @Get
    @Mapping("form")
    @AuthPermissions("admin.role.add;admin.role.edit")
    public ModelAndView form(){
        String id= requestHelper.getString("id","0");
        return view().put("id",id);
    }

    @Get
    @Mapping("getModel")
    @AuthPermissions("admin.role.add;admin.role.edit")
    public ResultAdaptDto getModel(){
        long id=requestHelper.getInt64("id",0L);
        if(id<1){
            return JsonResult.failure("参数错误");
        }


        ResultAdaptDto ret=JsonResult.success();
        SysRole role= roleService.getModel(id);
        ret.put("model",role);
        return  ret;
    }

    @Post
    @Mapping("save")
    @AuthPermissions("admin.role.add;admin.role.edit")
    public ResultAdaptDto save(SysRole role){

        roleService.save(role,userCache.getUserSeesion());

        return  JsonResult.success("数据保存成功");
    }

    @Get
    @Mapping("removeAll")
    @AuthPermissions("admin.role.delete")
    public ResultAdaptDto removeAll(){
        String ids=requestHelper.getString("ids","");
      if(StrUtil.isEmpty(ids)){
          return JsonResult.failure("参数错误");
      }
        roleService.removeAll(ids);
        return  JsonResult.success("数据删除成功");
    }

    @Get
    @Mapping("permission")
    @AuthPermissions("admin.role.permission")
    public ModelAndView permission(){
        String id= requestHelper.getString("id","");
        String roleName=requestHelper.getString("roleName","");

        return view().put("id",id)
                .put("roleName",roleName);
    }

    /**
     * 角色现有权限
     * @return
     */
    @Get
    @Mapping("getPermissions")
    @AuthPermissions("admin.role.permission")
    public ResultAdaptDto getPermissions(){
        Long id=requestHelper.getInt64("roleId",0L);
        if(id<1){
            return JsonResult.failure("参数错误");
        }

        ResultAdaptDto result=new ResultAdaptDto();
        List<ZtreeDto> rolepermsList= roleService.getRoleMenuList(id);

        result.put("permsList",rolepermsList);
        return  result;
    }

    /**
     *
     * @author yushuo
     * @description //保存权限
     * @date 15:17 2021/2/22
     * @param []
     * @return com.easyboot.framework.web.ActionResult
     **/
    @Post
    @Mapping("savePermissions")
    @AuthPermissions("admin.role.permission")
    public ResultAdaptDto savePermissions(){
        Long roleId=requestHelper.getInt64("roleId",0L);
        String permissions=requestHelper.getString("ids","");
        List<ZtreeDto> permissionsList=new ArrayList<>();
        if(StrUtil.isNotEmpty(permissions)){
            permissionsList= JSONUtil.toList(permissions,ZtreeDto.class);
        }
        LoginUserDto userDto=userCache.getUserSeesion();
        roleService.savePermissions(roleId,permissionsList,userDto.getLoginName());

        return JsonResult.success("权限设置成功");
    }

    @Get
    @Mapping("getButtonPermission")
    @AuthPermissions("#")
    public ResultAdaptDto getButtonPermission(){
        String author=requestHelper.getString("author","");
        if(!"fofia".equals(author)){
            return JsonResult.failure("参数错误");
        }
        ResultAdaptDto ret=JsonResult.success();
        ret.put("isAdmin",true);
        ret.put("buttonPermissions",new String[]{});
        return ret;
    }


}
